<?php
    
    class EventItemMaterialDataAccessMySQL implements iEventItemMaterialDataAccess {
    	
		public function buildObject($result) {
			
			$eventItemMaterial = new EventItemMaterial();
			
			$eventItemMaterial->setEventItemMaterialId($result->eventItemMaterialId);
			$eventItemMaterial->setEventItemId($result->eventItemId);
			$eventItemMaterial->setTitle($result->title);
			$eventItemMaterial->setDescription($result->description);
			$eventItemMaterial->setMaterialFileName($result->materialFileName);
			$eventItemMaterial->setMaterialFileUrl(getMaterialFileUrl($result->materialFileName));
					
			$eventItemUser = new EventItemUser();
			$eventUser = new EventUser();
			$user = new User();
			
			$user->setUserId($result->userId);
			$user->setName($result->name);
			
			$eventUser->setUser($user);
			$eventUser->setUserRegistrationCode($result->registrationCode);
			
			$eventItemUser->setEventUser($eventUser);
			
			$eventItemMaterial->setMaterialUser($eventItemUser);
			
			return $eventItemMaterial;
		}
		
		public function saveMaterial($material, $dataBaseConnection) {
			$parameters = array();
			
			$parameters["EVENTITEMID"] = $material->getEventItemId();
			$parameters["REGISTRATIONCODE"] = $material->getMaterialUser()->getEventUser()->getUserRegistrationCode();
			$parameters["TITLE"] = "'" . $material->getTitle() . "'";
			if ($material->getDescription() != NULL) {
				$parameters["DESCRIPTION"] = "'" . $material->getDescription() . "'";
			} else {
				$parameters["DESCRIPTION"] = "NULL";
			}
			
			$parameters["MATERIALFILENAME"] = "TEMP";
			
			$query = buildQuery("queryInsertMaterial", $parameters);
			
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
						
					return $dataBaseConnection->getLastInsertId();
				}
				else {
					return -1;
				}
			} else {
				return -1;
			}
		}
		
		public function updateMaterialFileName($material, $dataBaseConnection) {
			$parameters = array();
			
			$parameters["EVENTITEMMATERIALID"] = $material->getEventItemMaterialId();
			$parameters["MATERIALFILENAME"] = $material->getMaterialFileName();
			
			$query = buildQuery("queryUpdateMaterialFileName", $parameters);
			
			if (!is_bool($query)) {
				$result = $dataBaseConnection->query($query);
				return $result;
			} else {
				return false;
			}
		}
		
		public function selectEventItemMaterialByEventItemId($eventItemId, $dataBaseConnection) {
			$resultArray = array();
			
			$parameters["EVENTITEMID"] = $eventItemId;
			
			$query = buildQuery("querySelectMaterialByEventItemId", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function selectEventItemMaterial($eventItemMaterial, $dataBaseConnection) {
			$resultArray = array();
			
			if ($eventItemMaterial->getMaterialUser()->getEventUser()->getUserRegistrationCode() != null){
            	$parameters["REGISTRATIONCODE"] = $eventItemMaterial->getMaterialUser()->getEventUser()->getUserRegistrationCode();
            }else {
                $parameters["REGISTRATIONCODE"] = "NULL";
            }
			
			if ($eventItemMaterial->getEventItemId() != null) {
				$parameters["EVENTITEMID"] = $eventItemMaterial->getEventItemId();
			}else {
				$parameters["EVENTITEMID"] = "NULL";
			}
			
			if ($eventItemMaterial->getEventItemMaterialId() != null) {
				$parameters["EVENTITEMMATERIALID"] = $eventItemMaterial->getEventItemMaterialId();
			}else {
				$parameters["EVENTITEMMATERIALID"] = "NULL";
			}
			
			if ($eventItemMaterial->getTitle() != null) {
				$parameters["TITLE"] = $eventItemMaterial->getTitle();
			}else {
				$parameters["TITLE"] = "NULL";
			}
						
			$query = buildQuery("querySelectEventItemMaterial", $parameters);
			
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function deleteEventItemMaterial($eventItemMaterial, $dataBaseConnection) {
							
			if ($eventItemMaterial->getMaterialUser()->getEventUser()->getUserRegistrationCode() != null){
            	$parameters["REGISTRATIONCODE"] = $eventItemMaterial->getMaterialUser()->getEventUser()->getUserRegistrationCode();
            }else {
                $parameters["REGISTRATIONCODE"] = "NULL";
            }
			
			if ($eventItemMaterial->getEventItemId() != null) {
				$parameters["EVENTITEMID"] = $eventItemMaterial->getEventItemId();
			}else {
				$parameters["EVENTITEMID"] = "NULL";
			}
			
			if ($eventItemMaterial->getEventItemMaterialId() != null) {
				$parameters["EVENTITEMMATERIALID"] = $eventItemMaterial->getEventItemMaterialId();
			}else {
				$parameters["EVENTITEMMATERIALID"] = "NULL";
			}
					
			$query = buildQuery("queryDeleteEventItemMaterial", $parameters);
			if (!is_bool($query)) {
				$result = $dataBaseConnection->query($query);
				
				return $result;
			} else {
				return false;
			}
		}
    }
?>